﻿using System;
using System.Linq;
using Autofac;
using ComputerVision.BacChiuLuaForm;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.HangSanXuatForm;
using ComputerVision.PhieuDieuTraCoSoForm;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmHangMuc : frmDetail
    {
        private readonly IService<HangMuc> _hangMucService;
        private readonly IService<BacChiuLua> _bacChiuLuu;
        private readonly IService<HangSanXuat> _hangSanXuat;
        private frmPhieuDieuTraCoSoDetail _frmPhieuDieuTraCs;
        public Guid IdPhieu;
        private readonly IUserService<aspnet_Users> _userService;

        public frmHangMuc()
        {
            InitializeComponent();
            _hangMucService = Program.Container.Resolve<IService<HangMuc>>();
            _bacChiuLuu = Program.Container.Resolve<IService<BacChiuLua>>();
            _hangSanXuat = Program.Container.Resolve<IService<HangSanXuat>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Hạng mục";
            txtTenCongTrinh.Focus();
            LoadBacChiuLuu();
            LoadHangSanXuat();
        }

        public frmHangMuc(frmPhieuDieuTraCoSoDetail frm)
        {
            InitializeComponent();
            this._frmPhieuDieuTraCs = frm;
            _hangMucService = Program.Container.Resolve<IService<HangMuc>>();
            _bacChiuLuu = Program.Container.Resolve<IService<BacChiuLua>>();
            _hangSanXuat = Program.Container.Resolve<IService<HangSanXuat>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Hạng mục";
            txtTenCongTrinh.Focus();
            LoadBacChiuLuu();
            LoadHangSanXuat();            
        }

        public frmHangMuc(Guid id)
        {
            InitializeComponent();
            _hangMucService = Program.Container.Resolve<IService<HangMuc>>();
            _bacChiuLuu = Program.Container.Resolve<IService<BacChiuLua>>();
            _hangSanXuat = Program.Container.Resolve<IService<HangSanXuat>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Sửa Hạng mục";
            txtTenCongTrinh.Focus();
            LoadBacChiuLuu();
            LoadHangSanXuat();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _hangMucService.Detail(id);
            if (item != null)
            {
                txtTenCongTrinh.Text = item.TenCongTrinh;
                drlHangSanXuat.SelectedValue = item.HangSanXuat_Id;
                drlBacChiuLua.SelectedValue = item.BacChiuLua_Id;
                txtDienTichM2.Text = item.DienTichM2.ToString();
                txtSoTang.Text = item.SoTang.ToString();
                txtDienTichM3.Text = item.DienTichM3.ToString();
                txtSoPhong.Text = item.SoPhong.ToString();
                txtTenChatChay.Text = item.TenChatChay;
                txtDienTichChatChatM2.Text = item.DienTichChatChayM2.ToString();
                textGhiChu.Text = item.GhiChu;
            }
        }

        public void LoadBacChiuLuu()
        {
            drlBacChiuLua.DataSource = _bacChiuLuu.ToList().OrderBy(b => b.Ten).ToList();
            drlBacChiuLua.DisplayMember = "Ten";
            drlBacChiuLua.ValueMember = "Id";
        }

        public void LoadHangSanXuat()
        {
            drlHangSanXuat.DataSource = _hangSanXuat.ToList().OrderBy(h => h.Ten).ToList();
            drlHangSanXuat.DisplayMember = "Ten";
            drlHangSanXuat.ValueMember = "Id";
        }

        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtTenCongTrinh.Text))
            {
                errorProvider.SetError(txtTenCongTrinh, "Bạn chưa nhập tên công trình, nhà xưởng, kho bãi. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var hang = drlHangSanXuat.SelectedItem as HangSanXuat;
                var bac = drlBacChiuLua.SelectedItem as BacChiuLua;
                var model = new HangMuc
                {
                    Id = Guid.NewGuid(),
                    TenCongTrinh = txtTenCongTrinh.Text,
                    HangSanXuat_Id = hang.Id,
                    BacChiuLua_Id = bac.Id,
                    DienTichM2 = int.Parse(txtDienTichM2.Text),
                    SoTang = int.Parse(txtSoTang.Text),
                    DienTichM3 = int.Parse(txtDienTichM3.Text),
                    SoPhong = int.Parse(txtSoPhong.Text),
                    TenChatChay = txtTenChatChay.Text,
                    DienTichChatChayM2 = int.Parse(txtDienTichChatChatM2.Text),
                    GhiChu = textGhiChu.Text,
                    PhieuDieuTraCoSo_Id = this.IdPhieu,
                    Created = DateTime.Now,
                    CreatedBy_Id = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy_Id = Constants.CurrentUser
                };
                _hangMucService.Create(model);

                if (this._frmPhieuDieuTraCs != null)
                {
                    //this._frmPhieuDieuTraCs.LoadHangMuc();
                }
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _hangMucService.Detail(guid);
            var hang = drlHangSanXuat.SelectedItem as HangSanXuat;
            var bac = drlBacChiuLua.SelectedItem as BacChiuLua;
            if (item != null)
            {
                item.TenCongTrinh = txtTenCongTrinh.Text;
                item.HangSanXuat_Id = hang.Id;
                item.BacChiuLua_Id = bac.Id;
                item.DienTichM2 = int.Parse(txtDienTichM2.Text);
                item.SoTang = int.Parse(txtSoTang.Text);
                item.DienTichM3 = int.Parse(txtDienTichM3.Text);
                item.SoPhong = int.Parse(txtSoPhong.Text);
                item.TenChatChay = txtTenChatChay.Text;
                item.DienTichChatChayM2 = int.Parse(txtDienTichChatChatM2.Text);
                item.GhiChu = textGhiChu.Text;
                item.Modified = DateTime.Now;
                item.ModifiedBy_Id = Constants.CurrentUser;
                _hangMucService.Update(item);
            }
        }

        #endregion

        private void btnAddHangSanXuat_Click(object sender, EventArgs e)
        {
            var frm = new frmHangSanXuatDetail(this);
            frm.ShowDialog();
        }

        private void btnAddBacChiuLua_Click(object sender, EventArgs e)
        {
            var frm = new frmBacChiuLuaDetail(this);
            frm.ShowDialog();
        }
    }
}
